package com.adjy.pathbuilders.screens;

import java.util.LinkedList;
import java.util.List;

import android.view.MotionEvent;

import com.adjy.pathbuilders.objects.button.Button;
import com.adjy.pathbuilders.objects.button.ImageButton;
import com.adjy.pathbuilders.objects.button.listeners.BackToMainMenuButtonListener;
import com.adjy.pathbuilders.objects.button.listeners.ButtonListener;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.rendering.loading.WeakReferenceBitmap;
import com.adjy.pathbuilders.state.Game;
import com.adjy.pathbuilders.world.GameWorld;

/**
 */
public class GameOver implements GameScreen {

	private AnimationFactory animationFactory;
	private Game game;
	private GameWorld gameWorld;
	private List<Button> buttons;

	public GameOver(AnimationFactory anFactory, Game game, GameWorld world) {
		this.animationFactory = anFactory;
		this.game = game;
		this.gameWorld = world;
		this.buttons = new LinkedList<Button>();
	}
	
	private void createButtons() {
		if (buttons.size() == 0) {
			
			Button b = new ImageButton(240, 300,
					new BackToMainMenuButtonListener(game),
					new WeakReferenceBitmap(animationFactory, "menu_selected"),
					new WeakReferenceBitmap(animationFactory, "menu_unselected"));
			buttons.add(b);
			
			ButtonListener listener = new ButtonListener() {
				
				@Override
				public void onButtonPressed() {
					gameWorld.restart();
				}
			};
			
			b = new ImageButton(480, 300,
					listener,
					new WeakReferenceBitmap(animationFactory, "restart_selected"),
					new WeakReferenceBitmap(animationFactory, "restart_unselected"));
			buttons.add(b);
			
		}
	}

	/**
	 * This method performs one step.<br>
	 * 
	 * @param timePassed
	 *            Elapsed Time in Seconds
	 */
	public void step(float timePassed) {
		//FIXME Nothing in here
	}

	/**
	 * Method to receive any input events coming on the screen
	 * 
	 * @param event
	 *            Event from the screen
	 */
	@Override
	public void input(MotionEvent event) {
		
		for (Button b : buttons)
			b.input(event);
		
	}

	/**
	 * @param mcanvas
	 *            Canvas to be drawn on
	 */
	public void render(GameCanvas mcanvas, float timeElapsed) {
		mcanvas.drawBackground(this.animationFactory.getImage("game_over_bg")); // FIXME: this should be static
		
		for (Button b : buttons)
			b.render(mcanvas, timeElapsed);
	}

	@Override
	public void load() {
		createButtons();
		animationFactory.loadImages("game_over_bg");
	}

	@Override
	public void release() {
		buttons.clear();
		animationFactory.releaseImages("game_over_bg");
	}
	
	
}
